//
//  ContrastConfigView.swift
//  ChanMaMa
//
//  Created by 戴泉明 on 2021/4/21.
//  Copyright © 2021 蝉妈妈-戴泉明. All rights reserved.
//

import UIKit
import SnapKit

//声明代理
protocol ContrastScrollDelegate: class {
    func e_scrollViewDidScroll(_ scrollView: UIScrollView?)
}

class ContrastConfigView: ContrastConfigBaseView, UIScrollViewDelegate {
    
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect.zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.sectionHeaderHeight = 45
        tableView.estimatedRowHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.bounces = false
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        tableView.backgroundColor = self.backgroundColor
        tableView.contentInsetAdjustmentBehavior = .never
        return tableView
    }()
    
    var scrollView:UIScrollView = {
        let scroll = UIScrollView()
        scroll.bounces = false
        scroll.contentInsetAdjustmentBehavior = .never
        return scroll
    }()
    
    
    var datas: [ContrastModel] = []
    weak var delegate: ContrastScrollDelegate?
    weak var cellHeightDelegate: ContrastCellHeightDelegate?
    var dataArr: [GroupParamsModel] = []
    var hiddenHeader: CompareHiddenHeader = {
        let header = CompareHiddenHeader.create()
        header.frame = CGRect(x: 0, y: 0, width: CONTRAST_CONFIG_WIDTH, height: CONTRAST_HEADER_HEIGHT)
        header.showDifferenceBlock = {(show) in
            NotificationCenter.default.post(name: NSNotification.Name(rawValue:"showDifferenceContrastNotification"), object: nil, userInfo: ["show":show])
        }
        return header
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(scrollView)
        scrollView.addSubview(tableView)
        scrollView.addSubview(hiddenHeader)
        scrollView.snp.remakeConstraints { (make) in
            make.top.bottom.left.right.equalTo(self)
        }
        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(CONTRAST_HEADER_HEIGHT)
            make.bottom.left.right.equalTo(self)
        }
        
        self.backgroundColor = UIColor.clear
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        delegate?.e_scrollViewDidScroll(scrollView)
    }
    
    func setDatas(datas:[ContrastModel]) {
        self.datas = datas
        
        scrollView.contentSize = CGSize(width: 1, height: 1);
        tableView.snp.remakeConstraints { (make) in
            make.top.equalTo(CONTRAST_HEADER_HEIGHT)
            make.left.equalTo(scrollView)
            make.bottom.equalTo(self)//底部不能给到scrollview 否则出不来
            make.width.equalTo(2000)
        }

        dataArr = datas[0].groupParamsViewModelList
        tableView.reloadData()
        hiddenHeader.backgroundColor = UIColor.white
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}





extension ContrastConfigView:UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArr.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr[section].paramList.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return cellHeightDelegate?.e_tableViewCellHeight(indexPath) ?? 40
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = ContrastConfigCell.cellWithTableView(tableView: tableView)
        cell.titleLabel.text = dataArr[indexPath.section].paramList[indexPath.row].paramName
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionHeader = ContrastConfigHeaderView.create()
        sectionHeader.titleLabel.text = self.dataArr[section].groupName
        return sectionHeader
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
    }
}

